Table 21–2: Namespace variations
|
Method call example/ Resulting tag
|
Use case explanation
|
|
|
Case: You supply an element name, but no namespace prefix and no namespace URI.
Result: The element is written without a prefix and the default namespace will be used.
|
|
|
Case 1: You supply an element name with a namespace prefix, but no namespace URI. The supplied prefix has been previously associated with a namespace URI.
Result: The element is written with the supplied prefix and the previously associated namespace URI will be used.
|
|
|
Case 2: You supply an element name with a namespace prefix, but no namespace URI. The supplied prefix has not been previously associated with a namespace URI. The STRICT attribute is set to TRUE.
Result: The call generates an error. Only the default namespace can be set to an empty string (““).
|
|
|
Case 3: You supply an element name with a namespace prefix, but no namespace URI. The supplied prefix has not been previously associated with a namespace URI. The STRICT attribute is set to FALSE.
Result: The element is written with the supplied prefix and an empty namespace is specified.
|
|
|
Case 1: You supply an element name without a namespace prefix. You supply a namespaceUri that has been previously associated with a namespace prefix.
Result: The element is written using the previously associated prefix and the declared namespace URI is used.
|
|
|
Case 2: You supply an element name without a namespace prefix. You supply a namespaceUri that has not been previously associated with a namespace prefix.
Result: The element is written without a prefix and the namespaceUri is set to the namespace URI associated with the default namespace.
|
|
|
Case 1: You supply an element name with a namespace prefix and a namespaceUri that has been previously associated with the supplied prefix.
Result: The element is written with the supplied prefix and name and the previously associated namespace URI will be used. Since the namespace matching the supplied prefix and URI pair has already been declared, it will not be redeclared.
|
|
|
Case 2: You supply an element name with a namespace prefix and a namespaceUri. Either one or both of the namespace prefix and the namespace URI has already been used in a previous declaration, but the pair have not been declared together.
Result: The element is written with the supplied prefix and name and namespaceUri declared. This amounts to the declaration of a new namespace.
|
|
|
Case 3: You supply an element name with a namespace prefix and a namespaceUri. The namespace prefix matches the prefix used in the element, but the URIs do not match. The STRICT attribute is set to TRUE.
Result: The method call generates an error message. Within an element, namespaces are like attributes and must be unique.
Exception: In the case where the element’s URI is the empty string, the tag will be written with the supplied namespaceUri.
|
|
|
Case 4: You supply an element name with a namespace prefix and a namespaceUri. The namespace prefix matches the prefix used in the element, but the URIs do not match. The STRICT attribute is set to FALSE.
Result: The tag will be written, but it will not be valid XML.
|
|
|
Case 1: You supply a new prefix and new namespaceUri that do not overlap with those declared in the element.
Result: Creates the expected namespace without error.
|
|
|
Case 2: The provided namespaceUri matches the namespace URI declared in the element and prefix matches the namespace prefix declared in the element.
Result: Re-declaring an implicitly created namespace (the namespace declared in the element) does no harm and will not generate an error message.
|
|
|
Case 3: namespaceUri is provided and the namespace URI declared in the element is the empty string (““), and the provided prefix matches the namespace prefix declared in the element.
Result: The namespace is created without error. In other words, if you are going to explicitly create your namespaces, you do not need to provide the namespace URI when you create the element.
|
|
|
Case 4: namespaceUri is provided but it does not match the namespace URI declared in the element and the supplied prefix does match the namespace prefix declared in the element. The STRICT attribute is set to TRUE.
Result: The method call generates an error. Within an element, namespaces are like attributes and must be unique.
|
|
|
Case 1: You provide only a namespaceUri and an empty string (““) as the namespace prefix.
Result: You create the default namespace.
|
|
|
Case 2: The element has declared a default namespace with a different namespace URI than the one you provide with namespaceUri. The STRICT attribute is set to TRUE.
Result: The method call generates an error. Within an element, namespaces are like attributes and must be unique.
|
|
|
Case 3: The element has declared a default namespace with a different namespace URI than the one you provide with namespaceUri. The STRICT attribute is set to FALSE.
Result: The tag will be written, but it will not be valid XML.
|
|
|
Case 1: You provide an empty string (““) as the namespace URI and supply the prefix. The STRICT attribute is set to TRUE.
Result: The method call generates an error. Only the default namespace can be declared as the empty string (““).
|
|
|
Case 2: You provide an empty string (““) as the namespace URI and supply a prefix that matches the namespace prefix declared in the element. The STRICT attribute is set to FALSE.
Result: This can be useful when not running in STRICT mode to reset the element namespace prefix to the empty string (““).
|
|
|
Case 3: You provide an empty string (““) as the namespace URI and supply a prefix. The STRICT attribute is set to FALSE.
Result: The tag will be written, but it will not be valid XML.
|
|
|
Case 1: You provide an empty string (““) as the namespace URI and as the namespace prefix.
Result: You create the default namespace as empty.
|
|
|
Case 2: The element has declared a specific default namespace and you provide namespaceUri which is the empty string (““). The STRICT attribute is set to TRUE.
Result: The method call generates an error. Within an element, namespaces are like attributes and must be unique.
|
|
|
Case 3: The element has declared a specific default namespace and you provide namespaceUri which is the empty string (““). The STRICT attribute is set to FALSE.
Result: The tag will be written, but it will not be valid XML
|